<?php
namespace app\weixin\controller;
use think\Controller;
class Auth extends Controller
{
    protected function _initialize()
    {    
    444444444444444
		 $d['uid'] = 16;
         $d['nickname'] = "love";
         $d['headimgurl'] = 'http://wx.qlogo.cn/mmopen/vi_32/XFskLZxGqd0YHzxDctdkTXl4l5aqGiaBuZhIdtFkRfyiba1gMn6JCuq6T75Ysb8JicK6mO5qEFP5gK69TFZI0H9AQ/0';
         $d['openid'] = '1561515fsdfsdfds';
		 $d['key'] = 'E42EA884C058BEA5567A0FA22BAB914D' ;
         cookie('userinfo',$d,86400*7);
		
		 $userinfo = cookie('userinfo');
		 
         /*
         $userinfo = cookie('userinfo');
         if(empty($userinfo)){   
             $mid = input('param.mid',''); //上级盟主口令
             $rid = input('param.rid',''); //机器人ID
      			 if($mid && !$rid){
      				  $UsersModel = model('Users');
      			    $muser = $UsersModel->get_miduser($mid);
                $rid = $muser['robot_id'];
      			 }
             session('mengzhu',array('mid'=>$mid,'rid'=>$rid));
           	 $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
           	 session('redirect_rul',$url);
  			    $red_url = url('login/wx');
           	 $this->redirect($red_url); 
         }else{
             $ret = $this->user_check($userinfo);
             if(!$ret){
				        cookie('userinfo',Null);
                $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                session('redirect_rul',$url);
                //$red_url = url('login/wx');
                //$this->redirect($red_url);           
             }
         }
         */
         $this->wxuser = $userinfo;
    }
  
    /*验证用户cookie是否合法*/
    private function user_check($userinfo){
             $d['uid'] = $userinfo['uid'];
             $d['nickname'] = $userinfo['nickname'];
             $d['headimgurl'] = $userinfo['headimgurl'];
             $d['openid'] = $userinfo['openid'];
             $weixin = new \weixin\Weixin();
             $key = $weixin->MakeSign($d, config('user_key'));
             if($userinfo['key']==$key){
                return true;
             }else{
                return false;
             }
    }

}
